from sqlalchemy import Column, Integer, String, Text, Boolean, DateTime, JSON
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.sql import func
from app.db.session import Base


class XiaohongshuAccount(Base):
    """小红书账号模型"""
    __tablename__ = "xiaohongshu_accounts"
    
    id = Column(Integer, primary_key=True, index=True)
    
    # 核心账号信息
    nickname = Column(String(100), nullable=False, comment="昵称")
    account_name = Column(String(100), nullable=True, comment="账号名")
    
    # 统计数据
    followers = Column(String(20), default="0", comment="粉丝数（保持原始格式，如1.8万）")
    following = Column(String(20), default="0", comment="关注数（保持原始格式，如1.8万）")
    
    # 账号状态
    is_current = Column(Boolean, default=False, comment="是否为当前使用账号")
    
    # 登录信息
    cookies = Column(Text, nullable=True, comment="登录Cookie")
    last_login = Column(DateTime(timezone=True), nullable=True, comment="最后登录时间")
    
    # 时间戳
    created_at = Column(DateTime(timezone=True), server_default=func.now(), comment="创建时间")
    updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), comment="更新时间")


class XiaohongshuPublishLog(Base):
    """小红书发布日志模型"""
    __tablename__ = "xiaohongshu_publish_logs"
    
    id = Column(Integer, primary_key=True, index=True)
    account_id = Column(Integer, index=True, comment="账号ID")
    post_id = Column(Integer, index=True, comment="帖子ID")
    
    # 发布内容
    title = Column(String(200), nullable=False, comment="标题")
    content = Column(Text, nullable=False, comment="内容")
    images = Column(JSON, nullable=True, comment="图片列表")
    tags = Column(String(500), nullable=True, comment="标签")
    
    # 发布状态
    status = Column(String(20), default="pending", comment="发布状态: pending, publishing, success, failed")
    error_message = Column(Text, nullable=True, comment="错误信息")
    
    # 发布结果
    xiaohongshu_post_id = Column(String(100), nullable=True, comment="小红书帖子ID")
    xiaohongshu_url = Column(String(500), nullable=True, comment="小红书帖子URL")
    publish_time = Column(DateTime(timezone=True), nullable=True, comment="发布时间")
    
    # 统计数据
    views = Column(Integer, default=0, comment="浏览量")
    likes = Column(Integer, default=0, comment="点赞数")
    comments = Column(Integer, default=0, comment="评论数")
    shares = Column(Integer, default=0, comment="分享数")
    
    # 时间戳
    created_at = Column(DateTime(timezone=True), server_default=func.now(), comment="创建时间")
    updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), comment="更新时间")


class XiaohongshuToken(Base):
    """小红书Token模型"""
    __tablename__ = "xiaohongshu_tokens"
    
    id = Column(Integer, primary_key=True, index=True)
    account_id = Column(Integer, index=True, comment="账号ID")
    
    # Token信息
    token_type = Column(String(50), nullable=False, comment="Token类型: access_token, refresh_token, session_token")
    token_value = Column(Text, nullable=False, comment="Token值")
    expires_at = Column(DateTime(timezone=True), nullable=True, comment="过期时间")
    
    # Token状态
    is_active = Column(Boolean, default=True, comment="是否有效")
    last_used = Column(DateTime(timezone=True), nullable=True, comment="最后使用时间")
    
    # 额外信息
    scope = Column(String(200), nullable=True, comment="权限范围")
    token_metadata = Column(JSON, nullable=True, comment="元数据")
    
    # 时间戳
    created_at = Column(DateTime(timezone=True), server_default=func.now(), comment="创建时间")
    updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), comment="更新时间")